package com.fqdf.framework.android.view;

import android.os.CountDownTimer;
import android.widget.Button;
import android.widget.TextView;

import com.fqdf.framework.android.util.DateUtil;


public class MyCountTimer extends CountDownTimer {
    public static final long DEFAULT_TIME_COUNT = 60000;
    public static final long DEFAULT_TIME_INTERVAL = 1000;

    private TextView btn;

    private CountDownListener mListener;

    private boolean hourMinute;

    public interface CountDownListener {
        void finishCountDown();
    }

    /**
     * 参数 millisInFuture 倒计时总时间（如60S，120s等） 参数 countDownInterval 渐变时间（每次倒计1s） 参数
     * btn 点击的按钮(因为Button是TextView子类，为了通用我的参数设置为TextView）
     */
    public MyCountTimer(long millisInFuture, long countDownInterval, TextView btn) {
        super(millisInFuture, countDownInterval);
        this.btn = btn;
        btn.setText(String.valueOf(millisInFuture / 1000));
    }
    public MyCountTimer(long millisInFuture, long countDownInterval, TextView btn,boolean hourMinute) {
        super(millisInFuture, countDownInterval);
        this.btn = btn;
        this.hourMinute = hourMinute;

        btn.setText(DateUtil.remainingTimeStr(millisInFuture));
    }

    public MyCountTimer(Button btn) {
        super(DEFAULT_TIME_COUNT, DEFAULT_TIME_INTERVAL);
        this.btn = btn;
        btn.setText(String.valueOf(DEFAULT_TIME_COUNT / 1000));
    }

    public void setListener(CountDownListener listener) {
        mListener = listener;
    }

    // 计时完毕时触发
    @Override
    public void onFinish() {
        if (mListener != null) {
            mListener.finishCountDown();
        }
    }

    // 计时过程显示
    @Override
    public void onTick(long millisUntilFinished) {
        if(hourMinute){
            btn.setText(DateUtil.remainingTimeStr(millisUntilFinished));
        }else {
            btn.setText(String.valueOf(millisUntilFinished / 1000));
        }
    }
}
